package cn._51doit.flink.day03.transformations;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * 将一个POJO整体当成一个keyBy的条件
 */
public class KeyByDemo8 {


    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //spark,4
        //hive,5
        DataStreamSource<String> lines = env.socketTextStream("localhost", 8888);

        SingleOutputStreamOperator<WordCountBean> wordAndCount = lines.map(new MapFunction<String, WordCountBean>() {
            @Override
            public WordCountBean map(String line) throws Exception {
                String[] fields = line.split(",");
                String word = fields[0];
                int count = Integer.parseInt(fields[1]);
                //return new WordCountBean(word, count);
                return WordCountBean.of(word, count);
            }
        });

        KeyedStream<WordCountBean, WordCountBean> keyedStream = wordAndCount.keyBy(wc -> wc);

        keyedStream.print();

        env.execute();




    }

    public static class WordCount {

        private String word;

        private Integer count;

        public WordCount(){}


        public WordCount(String word, Integer count) {
            this.word = word;
            this.count = count;
        }

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }
    }


}
